package com.yikes.srb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.utils.object.BeanUtils;
import com.yikes.srb.leadingModle.form.transFlow.TransFlowAddForm;
import com.yikes.srb.leadingModle.form.transFlow.TransFlowEditForm;
import com.yikes.srb.leadingModle.form.transFlow.TransFlowQuery;
import com.yikes.srb.leadingModle.vo.TransFlowVO;
import com.yikes.srb.pojo.bo.TransFlowBO;
import com.yikes.srb.pojo.entity.TransFlow;
import com.yikes.srb.mapper.TransFlowMapper;
import com.yikes.srb.pojo.entity.UserInfo;
import com.yikes.srb.service.TransFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yikes.srb.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {

    private final UserInfoService userInfoService;

    @Override
    public PageResult<TransFlowVO> listPage(TransFlowQuery query) {

        Page<TransFlow> page = lambdaQuery()
                .eq(ObjectUtil.isNotNull(query.getUserId()), TransFlow::getUserId, query.getUserId())
                .like(StrUtil.isNotBlank(query.getUserName()), TransFlow::getUserName, query.getUserName())
                .like(StrUtil.isNotBlank(query.getTransNo()), TransFlow::getTransNo, query.getTransNo())
                .eq(ObjectUtil.isNotNull(query.getTransType()), TransFlow::getTransType, query.getTransType())
                .like(StrUtil.isNotBlank(query.getTransTypeName()), TransFlow::getTransTypeName, query.getTransTypeName())
                .page(Page.of(query.getPageNum(), query.getPageSize()));

        List<TransFlowVO> bean = BeanUtils.toBean(page.getRecords(), TransFlowVO.class);
        return PageResult.build(page.getTotal(), page.getPages(), bean);

    }

    @Override
    public TransFlowVO infoId(Long id) {

        TransFlow transFlow = getById(id);
        TransFlowVO bean = BeanUtils.toBean(transFlow, TransFlowVO.class);
        return bean;
    }

    @Override
    public void create(TransFlowAddForm form) {

        TransFlow bean = BeanUtils.toBean(form, TransFlow.class);
        save(bean);

    }

    @Override
    public void edit(TransFlowEditForm form) {

        TransFlow bean = BeanUtils.toBean(form, TransFlow.class);
        updateById(bean);
    }

    @Override
    public void del(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {

        //获取用户基本信息 user_info
        UserInfo userInfo = userInfoService.lambdaQuery().eq(UserInfo::getBindCode, transFlowBO.getBindCode()).one();

        //存储交易流水数据
        TransFlow transFlow = new TransFlow();
        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransNo(transFlowBO.getAgentBillNo());
        transFlow.setTransType(transFlowBO.getTransTypeEnum().getTransType());
        transFlow.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());
        transFlow.setTransAmount(transFlowBO.getAmount());
        transFlow.setMemo(transFlowBO.getMemo());
        baseMapper.insert(transFlow);

    }

    @Override
    public boolean isSaveTransFlow(String agentBillNo) {

        Long count = lambdaQuery().eq(TransFlow::getTransNo, agentBillNo).count();
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<TransFlowVO> selectByUserId(Long userId) {

        List<TransFlow> list = lambdaQuery().eq(TransFlow::getUserId, userId).orderByDesc(TransFlow::getCreatedTime).list();

        return BeanUtils.toBean(list, TransFlowVO.class);
    }
}
